﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using Microsoft.Practices.Unity;

namespace SpLocalizer.ContentSourceProviders.SpWeb
{
    public class SpUserResourceLocalizationTask : BaseTranslationTask
    {

        #region Constants
        private const string Property_ResourceKey = "ResourceKey";
        
        private const string Property_ResourceType = "ResourceType";

        internal const string WebTitle_ResourceName = "_WebTitle";

        internal const string WebDescription_ResourceName = "_WebDescription";

        /// <summary>
        /// Resource key for list description
        /// </summary>
        internal const string ListDescription_ResourceName = "_ListDescription";

        /// <summary>
        /// Resource key for list title
        /// </summary>
        internal const string ListTitle_ResourceName = "_ListTitle";

        /// <summary>
        /// Resource key for SPNavigationNode which is actualy in format _NavNode00000 where 00000 - is NavigationNodeId
        /// </summary>
        internal const string NavNode_ResourceName = "_NavNode";

        /// <summary>
        /// Resource key for List field title which is actualy in format _FieldTitleFieldInternalName 
        /// where FieldInternalName - is Internal name of the field
        /// </summary>
        internal const string FieldTitle_ResourceName = "_FieldTitle";

        /// <summary>
        /// Resource key for List field description which is actualy in format _FieldDescFielddInternalName 
        /// where Field InternalName - is Internal name of the field
        /// </summary>
        internal const string FieldDescription_ResourceName = "_FieldDesc";

        /// <summary>
        /// Resource key for content type description which is actualy in format _CTDesc0x00000 
        /// where 0x00000 - is content type id ContentTypeId
        /// </summary>
        internal const string ContentTypeDescription_ResourceName = "_CTDesc";

        /// <summary>
        /// Resource key for content type name which is actualy in format _CTName0x00000 
        /// where 0x00000 - is content type id ContentTypeId
        /// </summary>
        internal const string ContentTypeName_ResourceName = "_CTName";

        #endregion

        /// <summary>
        /// Respource key 
        /// </summary>
        public string ResourceKey
        {
            get {return this.GetPropertyValue(Property_ResourceKey);}
            set {this.SetPropertyValue(Property_ResourceKey,value);}
        }

        /// <summary>
        /// Respource key 
        /// </summary>
        public string ResourceType
        {
            get { return this.GetPropertyValue(Property_ResourceType); }
            set { this.SetPropertyValue(Property_ResourceType, value); }
        }

        /// <summary>
        /// Constructor without parameters should be public for unity injection
        /// </summary>
        public SpUserResourceLocalizationTask() { }

        internal SpUserResourceLocalizationTask(CultureInfo FromLanguage, CultureInfo ToLanguage, 
            SPUserResource resource) : base(FromLanguage,ToLanguage)
        {
            this.ResourceKey = resource.Name;
            this.SourceText = resource.GetValueForUICulture(FromLanguage);
            this.Status = TranslationTaskStatus.Initialized;
            this.ResourceType = resource.Type.ToString();
            this.FromLanguage = FromLanguage;
            this.ToLanguage = ToLanguage;

            string Translation = resource.GetValueForUICulture(ToLanguage);
            // Check if we already has translation for this language
            if (!String.IsNullOrEmpty(Translation) && !Translation.Equals(this.SourceText))
            {
                this.TranslatedText = Translation;
                this.Status = TranslationTaskStatus.Canceled; //We don't need dubplicate our job
            }
            
            
        }
   
    }
}
